#!/bin/bash

# very very alpha.

# 

# TODO(ecase): add command line options 
# TODO(ecase): add help
# TODO(ecase): figure out how to prompt for password only once?
# TODO(ecase): sort out filename convention
# TODO(ecase): incorporate version numbers into the LABEL
# TODO(ecase): use the variable names per rec-site-snapshot-procedure
# TODO(ecase): Comment code.
# TODO(ecase): Remove all of the PWDs or create a debugging / verbose mode
# TODO(ecase): Specify path to appcfg.py?
# TODO(ecase): Sort variable names coherently. 
# TODO(ecase): Acquire datastore kinds dynamically.
# TODO(ecase): Remove redundant code.
# TODO(ecase): Datastore / sqlite3 integrity checking? 
# TODO(ecase): Tarball automatically?
# TODO(ecase): ...?


gaeAuthEmail='ecase.aed@gmail.com'


SITE='rec-live'
#SITE='rec-beta'
#SITE='rec-dev'

dateStamp=`date +%Y-%m-%d`

LABEL=${SITE}.${dateStamp}

dataStores='Stats LabelInfo DocumentMetadata User'

localGAEDir='/home/ecase/code/glp-google/gae-global-learning-portal'

apiURL='http://global-learning-portal-2.appspot.com/admin/remote_api'


echo "Currently: `pwd`"
mkdir gae-ds.${LABEL}
cd gae-ds.${LABEL}
echo "Currently: `pwd`"

echo; echo; echo; echo; echo;

for DSK in `echo ${dataStores}`
do
	echo ${DSK}
	mkdir ${DSK}
	cd ${DSK}
	echo "Currently: `pwd`"
	echo "Running appcfg.py download_data --email=${gaeAuthEmail} --url=${apiURL} --kind=${DSK} --filename=gae-ds.${LABEL}.${DSK}.sql3 ${localGAEDir}"
	appcfg.py download_data --email=${gaeAuthEmail} --url=${apiURL} --kind=${DSK} --filename=gae-ds.${LABEL}.${DSK}.sql3 ${localGAEDir}
	cd ..
	echo "Currently: `pwd`"
	echo; echo; echo; echo; echo;


done

echo "Complete"
echo "Currently: `pwd`"
mkdir Complete
cd Complete
echo "Currently: `pwd`"
	echo "Running appcfg.py download_data --email=${gaeAuthEmail} --url=${apiURL} --filename=gae-ds.${LABEL}.Complete.sql3 ${localGAEDir}"
	appcfg.py download_data --email=${gaeAuthEmail} --url=${apiURL} --filename=gae-ds.${LABEL}.gae-ds.Complete.sql3 ${localGAEDir}
echo "Currently: `pwd`"

cd ..
echo "Currently: `pwd`"

cd ..
echo "Currently: `pwd`"

exit 0
